package 临时工具.粮仓.APDU;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class APDUClient {
    public static void main(String[] args) throws Exception {
        XMLBuilder xmlBuilder = new XMLBuilder("project");
        String message = xmlBuilder
                .add("dependencies")
                .add("dependencies.dependency[0].groupId", "hutool")
                .add("dependencies.dependency[0].artifactId", "hutool-all")
                .add("dependencies.dependency[0].version", "5.8.11")
                .add("dependencies.dependency[2].groupId", "junit")
                .add("dependencies.dependency[2].artifactId", "junit")
                .add("dependencies.dependency[2].version", "4.11")
                .add("dependencies.dependency[2].scope", "test")
                .build();



        Socket socket = new Socket("localhost", 30000);

        byte[] asduData = message.getBytes();

        short msgId = 100;
        byte[] apdu = APDUUtil.buildAPDU(msgId, asduData);

        OutputStream out = socket.getOutputStream();
        out.write(apdu);

        System.out.println("发送APDU成功。");
        // 接收服务端返回的 APDU 响应
        InputStream input = socket.getInputStream();
        byte[] responseBuffer = new byte[256];
        int bytesRead = input.read(responseBuffer);

        if (bytesRead != -1) {
            byte[] response = new byte[bytesRead];
            System.arraycopy(responseBuffer, 0, response, 0, bytesRead);
            System.out.print("服务端响应: "+new String(response));

        }
        socket.close();
    }
}
